package com.dlb.andorid.activity;

import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Build;
import androidx.annotation.Nullable;
import android.text.InputFilter;
import android.util.Log;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.dlb.andorid.R;
import com.dlb.andorid.XContract;
import com.dlb.andorid.XServerMsg;
import com.dlb.andorid.presenter.ReChargePresenter;
import com.kwan.xframe.common.widget.CashierInputFilter;
import com.kwan.xframe.mvp.presenter.BasePresenter;
import com.kwan.xframe.mvp.view.activity.BaseCommonActivity;
import com.kwan.xframe.util.DialogFactory;

import java.util.HashMap;
import java.util.Map;

public class RechargeActivity extends BaseCommonActivity implements XContract.IRechargeView {

	ReChargePresenter mPresenter;

	@Override
	protected BasePresenter getBasePresenter() {
		mPresenter = new ReChargePresenter(this);
		return mPresenter;
	}

	@Override
	protected void initData() {

	}

	@Override
	protected int getTopViewId() {
		return 0;
	}

	@Override
	protected int getMainViewId() {
		return R.layout.activity_recharge;
	}

	@Override
	protected int getBottomViewId() {
		return 0;
	}

	@Override
	protected String getTitleTxt() {
		return "充值";
	}

	@Override
	public String getPageName() {
		return null;
	}


	private TextView tv_pay;
	private EditText et_money;

	@Override
	protected void initViews() {
		super.initViews();
		et_money = findViewById(R.id.et_money);
		tv_pay = findViewById(R.id.tv_pay);

		InputFilter[] filters = {new CashierInputFilter()};
		et_money.setFilters(filters); //设置金额输入的过滤器，保证只能输入金额类型
		tv_pay.setOnClickListener(this);

	}

	public static final int PAY_TYPE_WX = 1;
	public static final int PAY_TYPE_ALI = 2;
	public int mCurrentPayType = PAY_TYPE_WX;

	@Override
	public void onClick(View v) {
		super.onClick(v);
		if (v == tv_pay) {

			final String strMoney = et_money.getText().toString();
			if (strMoney.isEmpty()) {
				toastMsg("请输入金额");
			} else {

				View view = getLayoutInflater().inflate(R.layout.layout_diaog_recharge, null);
				final Dialog dialog = DialogFactory.showMenuDialog(this, view);
				((TextView) view.findViewById(R.id.tv_money)).setText("¥ " + strMoney);

				view.findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						dialog.dismiss();
					}
				});

				view.findViewById(R.id.tv_pay).setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						dialog.dismiss();
						showProgress("");
						mPresenter.recharge(mCurrentPayType, Float.valueOf(strMoney));
					}
				});

				final CheckBox cb_ali = view.findViewById(R.id.cb_ali);
				final CheckBox cb_wx = view.findViewById(R.id.cb_wx);

				cb_ali.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

						if (b) {
							mCurrentPayType = PAY_TYPE_ALI;
							cb_wx.setChecked(false);
						}
					}
				});

				cb_wx.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

						if (b) {
							mCurrentPayType = PAY_TYPE_WX;
							cb_ali.setChecked(false);
						}
					}
				});

				dialog.show();


			}
		}
	}

	@Override
	public void getRechargeSuccess(XServerMsg data) {
		dismissProgress();

		if (data.getCode() == 0) {
			loadWeb(data.getMsg());
		} else {
			toastMsg(data.getMsg());
		}
	}

	protected WebView tbsWebView;

	protected void loadWeb(String loadUrl) {

		tbsWebView = findViewById(R.id.webView);
		WebSettings settings = tbsWebView.getSettings();
		settings.setJavaScriptEnabled(true);
		//https与http混合资源处理
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
			settings.setMixedContentMode(android.webkit.WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
		}
		// 设置加载进来的页面自适应手机屏幕
		settings.setUseWideViewPort(true);
		settings.setLoadWithOverviewMode(true);
		settings.setSupportZoom(true);
		settings.setBuiltInZoomControls(true);
		settings.setDisplayZoomControls(true);
		settings.setDefaultTextEncodingName("UTF-8");
		settings.setJavaScriptCanOpenWindowsAutomatically(true);

		//settings.setUserAgent( );
		final String Referer = "http://www.dilinbang.com";
		tbsWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
		tbsWebView.setWebViewClient(new WebViewClient() {

			@Override
			public void onLoadResource(WebView view, String url) {
				super.onLoadResource(view, url);
			}

			@Override
			public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
				super.onReceivedHttpError(view, request, errorResponse);
			}

			@Override
			public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
				handler.proceed();  // 接受所有网站的证书
				super.onReceivedSslError(view, handler, error);
			}

			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				Log.e("kwan", "shouldOverrideUrlLoading:" + url);
				if (url.startsWith("weixin://wap/pay?")) {
					Intent intent = new Intent();
					intent.setAction(Intent.ACTION_VIEW);
					intent.setData(Uri.parse(url));
					startActivityForResult(intent, 200);
					return true;
				} else {
					Map<String, String> extraHeaders = new HashMap<>();
					extraHeaders.put("Referer", Referer);
					view.loadUrl(url, extraHeaders);
					return true;
				}
			}
		});
		//需要添加到支付链接的请求头
		//String targetUrl=";  //后台返回的支付链接

		Map<String, String> extraHeaders = new HashMap<>();
		extraHeaders.put("Referer", Referer);
		tbsWebView.setWebChromeClient(new WebChromeClient() {
			@Override
			public void onProgressChanged(WebView view, int newProgress) {
				super.onProgressChanged(view, newProgress);
			}

		});

		tbsWebView.loadUrl(loadUrl, extraHeaders);

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		Log.e("kwan", "onActivityResult");
		if (requestCode == 200) {
			View view = getLayoutInflater().inflate(R.layout.dialog_pay_result, null);
			final Dialog dialog = DialogFactory.showCustomDialog3(this, view);
			view.findViewById(R.id.tv_pay_ok).setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					dialog.dismiss();
					finish();
				}
			});

			view.findViewById(R.id.tv_kf).setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					dialog.dismiss();
					go2Activity(KfActivity.class, null, false);
					finish();
				}
			});

			dialog.show();
		}
	}
}
